var msg_chart_not_loaded = "Something wrong...";

$(document).ready(function() {
	createIncomeDownChart();
	createProfitDownChart();
	createFundsShortageChart();
	createMaterialShortageChart();
	createTargetShortageChart();
	createBadDebtsAngularGaugeChart();
	createExceedPowerAngularGaugeChart();
	createPropertyRunoutAngularGaugeChart();
	createSlipAngularGaugeChart();
});

// 收入下滑
function createIncomeDownChart() {
	$.get(webContext + "/data/query.do",{
		"type" : "一收入下滑仪表",
		"oneDay" : false,
		"itemNames" : "收入计划,实际收入"
	},function(data) {
		createLineChart("incomeDown", data);
		createIncomeDownAngularGauge("incomeDown", data);
	});
}

// 利润下滑
function createProfitDownChart() {
	$.get(webContext + "/data/query.do",{
		"type" : "八利润下滑仪表",
		"oneDay" : false,
		"itemNames" : "利润计划,实际利润"
	},function(data) {
		createProfitDownLineChart("profitDown", data);
		createProfitDownAngularGauge("profitDown", data);
	});
}

// 资金短缺
function createFundsShortageChart() {
	var chartName = "fundsShortage";
	$.get(webContext + "/data/query.do",{
		"type" : "二资金短缺仪表",
		"itemNames" : "本日可用资金,本日资金差额"
	},function(data) {
		if (!data.success) {
			$("#" + chartName + "PieDiv").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "Pie2D.swf",
				chartName + "Pie", "350", "200", "0", "0");
		myChart.setDataXML(getFundsShortagePieChartXML(data.data));
		myChart.render(chartName + "PieDiv");
	});
}

//库存材料短缺
function createMaterialShortageChart() {
	var chartName = "materialShortage";
	$.get(webContext + "/data/query.do",{
		"type" : "三库存断货仪表",
		"itemNames" : "短缺品种数,材料所占比例,酒水所占比例"
	},function(data) {
		if (!data.success) {
			$("#" + chartName + "AngularGaugeDiv").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
				chartName + "AngularGauge", "350", "200", "0", "0");
		myChart.setDataXML(getMaterialShortageAngularGaugeChartXML(data.data));
		myChart.render(chartName + "AngularGaugeDiv");
		

		if (!data.success) {
			$("#" + chartName + "PieDiv").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "Pie2D.swf",
				chartName + "Pie", "350", "200", "0", "0");
		myChart.setDataXML(getMaterialShortagePieChartXML(data.data));
		myChart.render(chartName + "PieDiv");
	});
}

// 目标缺失
function createTargetShortageChart() {
	var chartName = "targetShortage";
	$.get(webContext + "/data/query.do",{
		"type" : "四目标缺失仪表",
		"itemNameLike" : "%偏差率" // TODO:SQL Injection
	},function(data) {
		if (!data.success) {
			$("#" + chartName + "ColumnDiv").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "Column3D.swf",
				chartName + "Column", "450", "300", "0", "0");
		myChart.setDataXML(getTargetShortageColumn3DChartXML(data.data));
		myChart.render(chartName + "ColumnDiv");
	});
}

// 呆死帐
function createBadDebtsAngularGaugeChart() {
	var chartName = "badDebts";
	$.get(webContext + "/data/query.do",{
		"type" : "五呆账死帐仪表",
		"itemNames" : "呆账总金额,呆账总笔数"
	},function(data) {
		if (!data.success) {
			$("#" + chartName + "AngularGaugeDiv").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
				chartName + "AngularGauge", "300", "300", "0", "0");
		myChart.setDataXML(getBadDebtsAngularGaugeChartXML(data.data));
		myChart.render(chartName + "AngularGaugeDiv");
		
		// 量杯图
		if (!data.success) {
			$("#" + chartName + "CylinderGaugeDiv").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "Cylinder.swf",
				chartName + "CylinderGauge", "110", "300", "0", "0");
		myChart.setDataXML(getBadDebtsCylinderGaugeChartXML(data.data));
		myChart.render(chartName + "CylinderGaugeDiv");
	});
}

// 审批越权
function createExceedPowerAngularGaugeChart(data) {
	var chartName = "exceedPower";
	$.get(webContext + "/data/query.do",{
		"type" : "六审批越权仪表",
		"itemNames" : "付款业务审批人越权次（笔）数,其他业务审批人越权次（笔）数"
	},function(data) {
		// 付款业务
		if (!data.success) {
			$("#" + chartName + "AngularGaugeDiv1").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
				chartName + "AngularGauge1", "400", "300", "0", "0");
		myChart.setDataXML(getExceedPowerAngularGaugeChartXML(data.data[0].itemValue, "付款业务"));
		myChart.render(chartName + "AngularGaugeDiv1");
		
		// 其它业务
		if (!data.success) {
			$("#" + chartName + "AngularGaugeDiv2").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
				chartName + "AngularGauge2", "410", "300", "0", "0");
		myChart.setDataXML(getExceedPowerAngularGaugeChartXML(data.data[1].itemValue, "其它业务"));
		myChart.render(chartName + "AngularGaugeDiv2");
	});
}

// 财产流失与积压
function createPropertyRunoutAngularGaugeChart() {
	var chartName = "propertyRunout";
	$.get(webContext + "/data/query.do",{
		"type" : "七财产流失仪表",
		"itemNames" : "盘亏品种数盘亏金额,积压品种数积压金额"
	},function(data) {
		// 流失
		if (!data.success) {
			$("#" + chartName + "AngularGaugeDiv1").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
				chartName + "AngularGauge1", "400", "300", "0", "0");
		myChart.setDataXML(getPropertyRunoutAngularGaugeChartXML(data.data[0].itemValue, "盘点情况"));
		myChart.render(chartName + "AngularGaugeDiv1");
		
		// 积压
		if (!data.success) {
			$("#" + chartName + "AngularGaugeDiv2").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
				chartName + "AngularGauge2", "410", "300", "0", "0");
		myChart.setDataXML(getPropertyRunoutAngularGaugeChartXML(data.data[1].itemValue, "积压情况"));
		myChart.render(chartName + "AngularGaugeDiv2");
	});
}

// 报表差错
function createSlipAngularGaugeChart() {
	var chartName = "slip";
	$.get(webContext + "/data/query.do",{
		"type" : "九报表误差仪表",
		"itemNames" : "本期差错笔数,误差率"
	},function(data) {
		// 笔数
		if (!data.success) {
			$("#" + chartName + "AngularGaugeDiv1").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
				chartName + "AngularGauge1", "400", "300", "0", "0");
		myChart.setDataXML(getSlipCountAngularGaugeChartXML(data.data[0].itemValue, "误差笔数"));
		myChart.render(chartName + "AngularGaugeDiv1");
		
		// 误差率
		if (!data.success) {
			$("#" + chartName + "AngularGaugeDiv2").html(msg_chart_not_loaded);
			return;
		}
		var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
				chartName + "AngularGauge2", "410", "300", "0", "0");
		myChart.setDataXML(getSlipRateAngularGaugeChartXML(data.data[1].itemValue, "误差率"));
		myChart.render(chartName + "AngularGaugeDiv2");
	});
}

function createLineChart(chartName, data) {
	if (!data.success) {
		$("#" + chartName + "LineDiv").html(msg_chart_not_loaded);
		return;
	}
	var myChart = new FusionCharts(chartsParent + "Line.swf",
			chartName + "LineChart", "350", "200", "0", "0");
	myChart.setDataXML(getLineChartXML(data));
	myChart.render(chartName + "LineDiv");
}

function createProfitDownLineChart(chartName, data) {
	if (!data.success) {
		$("#" + chartName + "LineDiv").html(msg_chart_not_loaded);
		return;
	}
	var myChart = new FusionCharts(chartsParent + "Line.swf",
			chartName + "LineChart", "350", "200", "0", "0");
	myChart.setDataXML(getProfitDownLineChartXML(data));
	myChart.render(chartName + "LineDiv");
}

function createIncomeDownAngularGauge(chartName, data) {
	if (!data.success) {
		$("#AngularGaugeDiv").html(msg_chart_not_loaded);
		return;
	}
	
	var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
			"incomeDownAngularGauge", "350", "200", "0", "0");
	myChart.setDataXML(getIncomeDownAngularGaugeXML(data));
	myChart.render("incomeDownAngularGaugeDiv");
}

function createProfitDownAngularGauge(chartName, data) {
	if (!data.success) {
		$(chartName + "#AngularGaugeDiv").html(msg_chart_not_loaded);
		return;
	}
	
	var myChart = new FusionCharts(chartsParent + "AngularGauge.swf",
			chartName + "AngularGauge", "350", "200", "0", "0");
	myChart.setDataXML(getProfitDownAngularGaugeXML(data));
	myChart.render(chartName + "AngularGaugeDiv");
}